// 昼夜切换与场景特效
import * as Cesium from 'cesium'

export function enableDayNight(viewer) {
  viewer.scene.postRender.addEventListener(() => {
    const sun =
      viewer.scene.light && viewer.scene.light.position
        ? viewer.scene.light.position
        : viewer.scene.sun.positionWC
    const camera = viewer.camera.positionWC
    if (!sun || !camera) return
    const sunDir = Cesium.Cartesian3.normalize(sun, new Cesium.Cartesian3())
    const camDir = Cesium.Cartesian3.normalize(camera, new Cesium.Cartesian3())
    const angle = Cesium.Cartesian3.dot(sunDir, camDir)
    if (angle < 0) {
      viewer.scene.skyAtmosphere.brightness = 0.3
      viewer.scene.globe.enableLighting = true
    } else {
      viewer.scene.skyAtmosphere.brightness = 1.0
      viewer.scene.globe.enableLighting = false
    }
  })
}
